package com.stylefeng.guns.modular.system.controller;

import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.Tools.CodeMsg;
import com.stylefeng.guns.core.common.Tools.Result;
import com.stylefeng.guns.core.common.annotion.BussinessLog;
import com.stylefeng.guns.modular.system.model.OrgcategoryFunction;
import com.stylefeng.guns.modular.system.service.IOrgcategoryFunctionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
/**
 * orgFunction控制器
 *
 * @author fengshuonan
 * @Date 2018-09-12 13:33:35
 */
@Controller
@RequestMapping(value={"/gunsApi/auth/orgcategoryFunction"})
@Api(value="机构分类功能权限相关api",tags={"机构分类功能权限操作接口"})
public class OrgcategoryFunctionController extends BaseController {

    @Autowired
    private IOrgcategoryFunctionService orgcategoryFunctionService;
    /*************************************sasazaixian@2018-10-09@start***********************************************************/
    /**
     * 根据机构分类查询所拥有的功能和操作
     *  @author sasazaixian@2018-10-05
     */
    @ApiOperation(value = "机构分类的功能和操作列表", notes = "机构分类的功能和操作列表", httpMethod = "GET")
    @GetMapping(value = "/treeList")
    @ResponseBody
    public Result treeList(@ApiParam(name = "soCate", value = "机构分类编码") @RequestParam(value = "soCate", required = true) String soCate) {
        try{
            return Result.success(orgcategoryFunctionService.selectListByCate(soCate));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.NOT_FIND_DATA, e.getMessage());
        }
    }
    /**
     * 保存机构分类的功能和操作
     *  @author sasazaixian@2018-10-05
     */
    @ApiOperation(value = "保存机构分类的功能和操作", notes = "保存机构分类的功能和操作", httpMethod = "POST")
    @PostMapping(value = "/save", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,consumes= MediaType.APPLICATION_JSON_UTF8_VALUE)
    @BussinessLog(value = "保存机构分类的功能和操作")
    @ResponseBody
    public Result save( @RequestBody @ApiParam(name = "ogcategoryFunction", value = "socId:机构分类id,objArrys:[{fid:功能id,sm:[操作id]-[操作id],socid:机构分类id}]")OrgcategoryFunction ogcategoryFunction, BindingResult result
    ) {
        //测试数据
        //{socId: '1',
        //objArrys:'[{fid:"7",sm:"",socid:"1"},
        //{fid:"8",sm:"",socid:"1"},
        //{fid:"9",sm:"",socid:"1"},
        //{fid:"10",sm:"",socid:"1"},
        //{fid:"11",sm:"",socid:"1"}]'
        //}
        if (result.hasErrors() || ogcategoryFunction==null || ogcategoryFunction.getSocId()==null || ogcategoryFunction.getSocId().trim().length()<1) {
            return Result.error(CodeMsg.SERVER_EXCEPTION,"参数异常！");
        }

        try{
            orgcategoryFunctionService.insertOrgcategoryFunction(ogcategoryFunction);
            return Result.success();
        }catch(Exception e){
            return Result.error(CodeMsg.NOT_FIND_DATA, e.getMessage());
        }
    }

}
